package ts1;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class FXMLMain extends Application {
    private Stage stage;
    public Sparkdata ts1=new Sparkdata();

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        stage.setTitle("SparkSQL 查询器");
        double MINIMUM_WINDOW_WIDTH = 200.0;
        stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
        double MINIMUM_WINDOW_HEIGHT = 200.0;
        stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
        gotologin();
        stage.show();
    }
    public void gotologin(){
        try {
            LoginController login = (LoginController) replaceSceneContent("FXML_LOGIN.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(FXMLMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void gotomain(){
        try {
            MainController main = (MainController) replaceSceneContent("FXML_MAIN.fxml");
            main.setApp(this);

        } catch (Exception ex) {
            Logger.getLogger(FXMLMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void userlogin(String urln ,String DCName, String user,String password){

        if(ts1.checkreturn (urln,DCName,user,password)){
            gotomain();
        }
    }
    public ObservableList<String> getTables() throws SQLException {

        Statement statement = ts1.getConnecton().createStatement();
        ResultSet res = statement.executeQuery("show tables");
        ObservableList<String> tables = FXCollections.observableArrayList();
        while(res.next()){
            tables.add(res.getString(1));
        }
        return tables;
    }


    public ResultSet getResult(String SQL)throws SQLException{
        Statement statement = ts1.getConnecton().createStatement();
        ResultSet res = statement.executeQuery(SQL);
        return res;
    }



    public void useroutmain(){
        gotologin();
    }
    private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = FXMLMain.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(FXMLMain.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 800, 600);
        stage.setScene(scene);
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }
    public static void main(String[] args) {
        launch(args);
    }
}